#!/bin/sh
#100228 BK support default network tool.
#100325 BK support Simple Network Setup.
#16aug10 shinobar: netchoice. BK: but only if defaultconnect still set to 'exec connectwizard'.
#110505 support sudo for non-root user.
#130104 rerwin: add frisbee
#130117 rerwin: remove redundant CURREXEC test, per shinobar
#160120 rerwin: add use of new pgprs and frisbee interfaces; correct frisbee check; ignore frisbee if not active.
#170308 rerwin: update filename in frisbee-active test; use only new frisbee (1.4+) and pgprs (2.0+) interfaces; remove gkdial check.
#170309 rerwin: retain choice in case multiple setups tried; end use of non-default exec when starting default exec.
#170515 rerwin: add checks for netwiz & sns installed.
#170724 rerwin: accommodate absence of a current exec, for default eth0, to connect/disconnect without using network wizard component.

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505

CURRENT_EXEC='' #170309... 170724
if [ -f /root/.connectwizardrc ];then
 . /root/.connectwizardrc #sets CURRENT_EXEC
fi

if [ "$CURRENT_EXEC" = "connectwizard" ];then #BK, 170309 end
  NETCHOICE=""
  #try determine which tool was used to setup networking...
  if [ -x /usr/local/bin/frisbee ] \
    && frisbee --test_active; then #130104 160120 170308
   NETCHOICE='frisbee' #130104 160120
  elif [ -x /usr/sbin/sns ] \
    && [ -s /etc/simple_network_setup/connections ];then #100306 160120 170515
   NETCHOICE='sns'
  elif [ -x /usr/sbin/net-setup.sh ] \
    && [ "`ls -1 /etc/network-wizard/network/interfaces 2>/dev/null`" ];then #170515
   NETCHOICE='net-setup.sh'
  fi
  [ -n "$NETCHOICE" ] && CURRENT_EXEC="$NETCHOICE" #170309
fi
#16aug10 shinobar: end

RUNMODE="$1"

case $RUNMODE in
  --wizard) exec /usr/sbin/connectwizard 1>&2 ;;
  --connect)
   case "$CURRENT_EXEC" in #connect using default tool. 170309
    net-setup.sh) exec /etc/rc.d/rc.network connect 1>&2 ;; #Dougal.
    pgprs) pgprs --connect >/dev/null ;; #160120 170308
    pupdial) pupdial ;;
    pppoe_gui) pppoe_gui ;;
    connectwizard) connectwizard ;;
    sns) /usr/local/simple_network_setup/rc.network ;;
    frisbee) frisbee --connect ;; #130104 160120 170308
    ?*) connectwizard ;; #170724
    *) exec /etc/rc.d/rc.network_eth ;; #170724
   esac
   ;;
  --disconnect) #disconnect using default tool.
   case "$CURRENT_EXEC" in #170309
    net-setup.sh) exec /etc/rc.d/rc.network stop 1>&2 ;; #Dougal.
    pgprs) pgprs --disconnect >/dev/null ;;  #160120 170308
    pupdial) killall wvdial; killall pppd ;;
    pppoe_gui) pppoe_gui ;;
    connectwizard) connectwizard ;;
    sns) /usr/local/simple_network_setup/rc.network stop ;;
    frisbee) frisbee --disconnect ;; #130104 160120 170308
    *) networkdisconnect --current-exec ;; #170724
  esac
   ;;
  *) exec /usr/local/bin/defaultconnect 1>&2 ;;
esac

###END###
